package com.cssy.xxkj.basic.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.byd.acegiext.domain.User;
import com.cssy.xxkj.basic.dao.BasicMaterialsTypeDAO;
import com.cssy.xxkj.basic.entity.BasicMaterialsTypeDO;
import com.cssy.xxkj.basic.service.IBasicMaterialsTypeService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.IDCodeTypeUtil;


@Service("basicMaterialsTypeService")
public class BasicMaterialsTypeServiceImpl implements IBasicMaterialsTypeService{

	
	@Resource(name="basicMaterialsTypeDAO")	
	private BasicMaterialsTypeDAO basicMaterialsTypeDAO;
	
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<BasicMaterialsTypeDO>  list=basicMaterialsTypeDAO.find(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			for(int i=0;i<list.size();i++){
		       	 	if(list.get(i).getZd003ParentId()!=null&&!"".equals(list.get(i).getZd003ParentId())){
		       	 		Map<String,BasicMaterialsTypeDO> map =this.basicMaterialsTypeDOlist();
		       	 		list.get(i).setBasicMaterialsTypeDO(map.get(list.get(i).getZd003ParentId()));
		       	 	}
			}
		}
		rs.setRecordCount(basicMaterialsTypeDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		return basicMaterialsTypeDAO.deleteBasicMaterialsTypeDOByPrimaryKey(id);
	}

	@Override
	public Integer update(BasicMaterialsTypeDO t, User user) throws SyBaseException {
		t.setBizModifyName(user.getName());
		t.setBizModifyDate(new Date());
		t.setBizVersionMark(t.getBizVersion());
		t.setBizVersion(t.getBizVersion()+1);
		return basicMaterialsTypeDAO.updateBasicMaterialsTypeDO(t);
	}

	@Override
	public BasicMaterialsTypeDO save(BasicMaterialsTypeDO t, User user)
			throws SyBaseException {
		String id  = IDCodeTypeUtil.getId();
		t.setId(id);
		t.setBizCreatorDate(new Date());
		t.setBizCreatorName(user.getName());
		t.setBizVersion(1);
		basicMaterialsTypeDAO.insertBasicMaterialsTypeDO(t);
		return t;
	}

	@Override
	public BasicMaterialsTypeDO findListById(String id) throws SyBaseException {
		BasicMaterialsTypeDO basicMaterialsTypeDO =basicMaterialsTypeDAO.findBasicMaterialsTypeDOByPrimaryKey(id);
		if(basicMaterialsTypeDO!=null&&basicMaterialsTypeDO.getId()!=null){
			if(basicMaterialsTypeDO.getZd003ParentId()!=null&&!"".equals(basicMaterialsTypeDO.getZd003ParentId())){
       	 		Map<String,BasicMaterialsTypeDO> map =this.basicMaterialsTypeDOlist();
       	 		basicMaterialsTypeDO.setBasicMaterialsTypeDO(map.get(basicMaterialsTypeDO.getZd003ParentId()));
       	 	}
		}
		return basicMaterialsTypeDAO.findBasicMaterialsTypeDOByPrimaryKey(id);
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return basicMaterialsTypeDAO.deleteBizRemoved(id);
	}

	@Override
	public List<BasicMaterialsTypeDO> findList(SearchConditions scs)
			throws SyBaseException {
		List<BasicMaterialsTypeDO> list=basicMaterialsTypeDAO.find(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			for(int i=0;i<list.size();i++){
		       	 	if(list.get(i).getZd003ParentId()!=null&&!"".equals(list.get(i).getZd003ParentId())){
		       	 		Map<String,BasicMaterialsTypeDO> map =this.basicMaterialsTypeDOlist();
		       	 		list.get(i).setBasicMaterialsTypeDO(map.get(list.get(i).getZd003ParentId()));
		       	 	}
			}
		}
		return list;
	}

	@Override
	public List<BasicMaterialsTypeDO> findParentId(String pid)
			throws SyBaseException {
		return basicMaterialsTypeDAO.findParentId(pid);
	}
	
	private Map<String,BasicMaterialsTypeDO> basicMaterialsTypeDOlist()throws DataAccessException{
		List<BasicMaterialsTypeDO> list =basicMaterialsTypeDAO.findListByExample(null);
		Map<String,BasicMaterialsTypeDO> map = new HashMap<String, BasicMaterialsTypeDO>();
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			for(int i=0;i<list.size();i++){
				map.put(list.get(i).getId(), list.get(i));
			}
		}else{
			return null;
		}
		return map;
	}
}
